<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <template #title >
        <div class="title-text">我的频道</div>
      </template>
      <van-button type="danger" class="edit-btn" round plain size="mini"
      @click="isEdit = !isEdit">{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
      @click="onChannelClick(obj,index)"
      class="grid-item"
      v-for="(obj,index) in myChannel" :key="obj.id">
        <template #text>
          <span class="text" :class="{active:index === active}">{{obj.name}}</span>
        </template>
        <template #icon>
          <van-icon name="clear" v-show="isEdit && !fixedChannels.includes(obj.id)"></van-icon>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
    <van-cell>
       <template #title >
        <div class="title-text">频道推荐</div>
      </template>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid" >
      <van-grid-item class="grid-item"
      v-for="item in recommendChannels"
      :key="item.id"
      @click="addChannel(item)"
      icon="plus" :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannels, delUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'channelEdit',
  components: {},
  props: {
    myChannel: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allChannel: [],
      isEdit: false,
      fixedChannels: [0]
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      const channels = []
      this.allChannel.forEach(item => {
        const flag = this.myChannel.some(val => val.id === item.id)
        if (!flag) {
          channels.push(item)
        }
      })
      return channels
    }
    // 方法二
    // recommendChannel() {
    //   return this.allChannel.filter(item => !this.myChannel.some(val => val.id === item.id))
    // }
  },
  watch: {},
  created() {
    this.loadAllChannels()
  },
  mounted() {},
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels()
        // console.log(data.data.channels);
        this.allChannel = data.data.channels
      } catch (error) {
        this.$toast('获取全部频道数据失败！')
      }
    },
    async addChannel(item) {
      //  console.log(item);
      this.myChannel.push(item)
      //  数据持久化存储
      // console.log(this.user);
      if (this.user) {
        // 登录状态
        try {
          await addUserChannels({
            id: item.id,
            seq: this.myChannel.length
          })
          this.$toast('更新服务器数据成功！')
        } catch (error) {
          console.log(error)
          this.$toast('更新服务器数据失败！')
        }
      } else {
        // 未登录状态
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    },
    onChannelClick(obj, ind) {
      //  console.log(obj,ind);
      if (this.isEdit) {
        // if(ind === 0) {
        //   return
        // }
        if (this.fixedChannels.includes(obj.id)) {
          return
        }
        // 出于编辑状态
        this.myChannel.splice(ind, 1)
        //
        if (ind <= this.active) {
          this.$emit('updateActive', this.active - 1, true)
        }
        // 调用删除channel方法
        this.deleteChannel(obj)
      } else {
        // 出于非编辑状态
        this.$emit('updateActive', ind, false)
      }
    },
    //  删除channel 持久化存储数据 方法
    async deleteChannel(obj) {
      if (this.user) {
        // 登录状态
        try {
          await delUserChannel(obj.id)
          this.$toast('删除频道数据成功！')
        } catch (error) {
          console.log(error)
          this.$toast('删除频道数据失败！')
        }
      } else {
        // 未登录状态
        this.$toast('删除频道数据成功！')
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
 .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text, .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
   /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
   /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
